//@原文链接：https://blog.csdn.net/ITSOK_123/article/details/124055730
//链表对齐解法
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* curA = headA;
        ListNode* curB = headB;
        int lenA = 0,lenB=0;
        while(curA != nullptr){
            lenA++;
            curA = curA->next;
        }
        while(curB!=nullptr){
            lenB++;
            curB = curB->next;
        }
        curA = headA;
        curB = headB;
        while(lenA<lenB){
            curB=curB->next;
            lenB--;
        }
        while(lenA>lenB){
            curA=curA->next;
            lenA--;
        }
        while(curA != nullptr){
            if(curA==curB){
                return curA;
            }
            curA = curA->next;
            curB = curB->next;
        }
        return NULL;
    }
};


//哈希表集合——空间换时间
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        unordered_set<ListNode*> visited;
        ListNode* curA = headA;
        ListNode* curB = headB;
        while(curA != nullptr){
            visited.insert(curA);
            curA = curA->next;
        }
        while(curB != nullptr){
            if(visited.count(curB))
                return curB;
            curB = curB->next;
        }
        return nullptr;
    }

//双指针解法——循环增长对齐链表
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if(headA == nullptr ||headB==nullptr){
            return nullptr;
        }
        ListNode* cura = headA;
        ListNode* curb = headB;
        while(cura != curb){
            cura = cura==nullptr?headB:cura->next;
            curb = curb==nullptr?headA:curb->next;
        }
        return cura;
    }
